using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Microsoft.Win32;

namespace hosted_gmail_client
{
    public class Common
    {
        public static RegistryKey GetRegistryKey(bool Editable)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\aimx\\hostedgmailclient", Editable);

            if (key == null) {
               key = Registry.CurrentUser.CreateSubKey("Software\\aimx\\hostedgmailclient");
               key = Registry.CurrentUser.OpenSubKey("Software\\aimx\\hostedgmailclient",true);
               key.SetValue("emailaddress","");
               key.SetValue("password","");
               key = Registry.CurrentUser.OpenSubKey("Software\\aimx\\hostedgmailclient",Editable);
            }

            return key;

        }

        public static bool AuthenticateUser(string emailaddress, string password)
        {
            RegistryKey key = Common.GetRegistryKey(false);

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://mail.google.com/hosted/" + key.GetValue("emailaddress").ToString().Split(Char.Parse("@"))[1] + "/feed/atom");
                req.Method = "GET";
                req.Credentials = new NetworkCredential(emailaddress, password);

                if (((HttpWebResponse)req.GetResponse()).StatusCode == HttpStatusCode.OK)
                    return true;
                else
                    return false;

            }
            catch
            {
                return false;
            }
        }
    }
}
